/*
 * SunSpotHostApplication.java
 *
 * Created on Jul 21, 2009 4:45:20 PM;
 */
package org.sunspotworld;

import com.sun.spot.core.resources.Resources;
import com.sun.spot.ieee_802_15_4_radio.IRadioPolicyManager;
import com.sun.spot.ieee_802_15_4_radio.util.IEEEAddress;
import com.sun.spot.multihop.io.j2me.radiogram.RadiogramConnection;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.microedition.io.Connector;
import javax.microedition.io.Datagram;

/**
 * Sample Sun SPOT host application
 */
public class SunSpotHostApplication {
	
	private static final String DATABASE_HOST = "YOUR_TEAM_LAMP_ADDRESS";
	private static final String DATABASE_NAME = "YOUR_DB_NAME";
	private static final String DATABASE_USERNAME = "YOUR_TEAM_MYSQL_ACCOUNT";
	private static final String DATABASE_PASSWORD = "YOUR_TEAM_MYSQL_PASSWORD";

	private Connection dbConn;
	private Statement dbStmt;

	/**
	 * Print out our radio address.
	 */
	public void run() {
		long ourAddr = ((IRadioPolicyManager) Resources.lookup(IRadioPolicyManager.class)).getIEEEAddress();
		System.out.println("Our radio address = " + IEEEAddress.toDottedHex(ourAddr));

		initDatabase();

		RadiogramConnection conn;
		Datagram dg;
		try {
			conn = (RadiogramConnection) Connector.open("radiogram://:87");
			dg = conn.newDatagram(conn.getMaximumLength());
		} catch (IOException ex) {
			Logger.getLogger(SunSpotHostApplication.class.getName()).log(Level.SEVERE, null, ex);
			return;
		}

		try {
			while (true) {
				conn.receive(dg);
				System.out.println("Received packet");

				String mac = dg.getAddress();
				long time = dg.readLong();
				int light = dg.readInt();

				insert(mac, time, light);
			}
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
	}

	private void initDatabase() {
		try {
			// This finds the driver to connect to the MySQL database
			Class.forName("com.mysql.jdbc.Driver").newInstance();

			// Open a connection to the server, use database "database", with username "user" and password "pass"
			dbConn = DriverManager.getConnection("jdbc:mysql://" + DATABASE_HOST + "/" + DATABASE_NAME +
					"?user=" + DATABASE_USERNAME + "&password=" + DATABASE_PASSWORD);

			dbStmt = dbConn.createStatement();

			dbStmt.execute("CREATE TABLE IF NOT EXISTS mydata (mac char(19), time bigint, light smallint)");
		} catch (IllegalAccessException ex) {
			ex.printStackTrace();
			System.exit(1);
		} catch (ClassNotFoundException ex) {
			ex.printStackTrace();
			System.exit(1);
		} catch (SQLException ex) {
			ex.printStackTrace();
			System.exit(1);
		} catch (InstantiationException ex) {
			ex.printStackTrace();
			System.exit(1);
		}
	}

	private void insert(String mac, long time, int light) {
		try {
			String tuple = "('" + mac + "', " + time + "," + light + ")";
			System.out.println("(MAC, time, light) = " + tuple);
			dbStmt.execute("INSERT INTO mydata VALUES " + tuple);
		} catch (SQLException ex) {
			Logger.getLogger(SunSpotHostApplication.class.getName()).log(Level.SEVERE, null, ex);
		}
	}

	public static void main(String[] args) {
		SunSpotHostApplication app = new SunSpotHostApplication();
		app.run();
	}
}
